//
//  JBPreviewViewController.swift
//  joyBao
//
//  Created by Chen on 2017/7/29.
//  Copyright © 2017年 Chen. All rights reserved.
//

import UIKit
import SnapKit

class JBPreviewViewController: BBBaseViewController {

    fileprivate var collectionView: UICollectionView!
    fileprivate var images: Array<Any>!
    fileprivate var currentIndex: Int?
    fileprivate var pageCount: UILabel?
    fileprivate var showPageCount: Bool = false//是否显示顶部页码

    init(image: Any) {
        super.init(nibName: nil, bundle: nil)

        self.images.append(image)
    }

    init(images: Array<Any>, index: Int) {
        super.init(nibName: nil, bundle: nil)

        self.images = images
        self.currentIndex = index
        self.showPageCount = (self.images.count > 1)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)

        self.navigationController?.setNavigationBarHidden(false, animated: true)
    }

    override var prefersStatusBarHidden: Bool {
        return true
    }

    override func setupUI() {
        super.setupUI()

        self.navigationController?.setNavigationBarHidden(true, animated: true)

        let collectionViewFlowLayout = UICollectionViewFlowLayout()
        collectionViewFlowLayout.scrollDirection = .horizontal
        collectionViewFlowLayout.minimumLineSpacing = 0
        collectionViewFlowLayout.minimumInteritemSpacing = 0
        collectionViewFlowLayout.itemSize = CGSize(width: ScreenWidth + 10, height: ScreenHeight)
        self.collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: collectionViewFlowLayout)
        self.collectionView.showsHorizontalScrollIndicator = false
        self.collectionView.scrollsToTop = false
        self.collectionView.backgroundColor = UIColor.black
        self.collectionView.isPagingEnabled = true
        self.collectionView.delegate = self as UICollectionViewDelegate
        self.collectionView.dataSource = self as UICollectionViewDataSource
        self.collectionView.register(JBPreviewCollectionCell.self, forCellWithReuseIdentifier: NSStringFromClass(JBPreviewCollectionCell.self))
        self.view.addSubview(self.collectionView)
        self.collectionView.snp.makeConstraints({ (make) in
            make.top.bottom.equalTo(self.view)
            make.left.equalTo(self.view.snp.left).offset(-5)
            make.width.equalTo(self.view.width + 10)
        })

        self.pageCount = UILabel()
        self.pageCount?.textAlignment = .center
        self.pageCount?.textColor = UIColor.white
        self.pageCount?.font = Font(font: 16)
        self.pageCount?.isHidden = !self.showPageCount
        self.pageCount?.backgroundColor = UIColor.black.withAlphaComponent(0.3)
        self.pageCount?.text = "\(self.currentIndex! + 1) / \(self.images.count)"
        self.view.addSubview(self.pageCount!)
        self.pageCount?.snp.makeConstraints({ (make) in
            make.left.top.right.equalTo(self.view)
            make.height.equalTo(40)
        })

        let offsetX = self.currentIndex! * Int(ScreenWidth + 10)
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
            self.collectionView.setContentOffset(CGPoint(x: offsetX, y: 0), animated: false)
        }
    }
}

extension JBPreviewViewController: UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.images.count
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: NSStringFromClass(JBPreviewCollectionCell.self), for: indexPath) as! JBPreviewCollectionCell
        cell.updateCell(image: self.images[indexPath.item])
        weak var weakSelf = self
        cell.singleTapCallback = {
            weakSelf?.navigationController?.popViewController(animated: true)
        }
        return cell
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        self.navigationController?.popViewController(animated: true)
    }

    func scrollViewDidScroll(_ scrollView: UIScrollView) {

        let index = Int(scrollView.contentOffset.x / (ScreenWidth + 10) + 0.5)
        if (index < 0 || index >= self.images.count) {
            return
        }
        self.pageCount?.text = "\(index + 1) / \(self.images.count)"
    }
}
